<?php
header ( 'Content-type:application/xml' );
$__l = ea_read_language($__f_lang_setting, 'st_common');
$categorys = cache_read('goods_categorys.php');

$oGoods = new Goods();
$oGoodscat = new Goodscat();

$sitemap_index = array(
	//WEB_URL."sitemap.xml",			// 原来这样做是禁止的
	WEB_URL."sitemap_google_pages.xml",	// 列表页
);

// 商品
$__page = 1;
$__pagesize = 100;
while ($list = $oGoods->get_list ( '*', array(), array(), $__page, $__pagesize)){
	if(empty($list['list'])){
		break;
	}
	
	$map_arr_goods = array();
	foreach ($list['list'] as $goods){
		$priority = number_format($goods['hits'] / 10 + 0.5, 1);
		if($priority > 1){
			$priority = 1;
		}
		$time = empty($goods['updatetime']) ? $goods['addtime'] : $goods['updatetime'];
		$lastmod = date('Y-m-d\TH:i:s', $time).substr(date("O", $time), 0, 3).":".substr(date("O", $time), 3);
		if(empty($goods['filename'])){
			$url = WEB_URL.'goods/'.time_to_path($goods['addtime']).'/'.$goods['id'].'.html';
		}else{
			$url = WEB_URL.'goods/'.$goods['filename'].'.html';
		}
		$map_arr_goods[] = array(
			'loc'		=> htmlspecialchars($url),
			'lastmod'	=> $lastmod,
			'changefreq'=> 'daily',
			'priority' 	=> $priority,
		);
	}
	$vars['sitemaps'] = $map_arr_goods;
	// 生成静态文件
	$html_data = $T->fetch('goods_sitemapGoogleGoods.xml');
	$filename = WEB_PTH."sitemap-{$__page}.xml";
	File::makeHtml($filename, $html_data);
	
	$sitemap_index[] = WEB_URL."sitemap-{$__page}.xml";
	$__page ++;
}

// 输出 SiteMap 索引文件
$sitemap_index = format_maps($sitemap_index, true);
$vars['sitemaps'] = $sitemap_index;
echo $html_data = $T->fetch('goods_sitemapGoogleIndex.xml');
$filename = WEB_PTH."sitemap_google.xml";
File::makeHtml($filename, $html_data);

// 提交给 Yahoo
$filename = WEB_PTH."sitemap_yahoo.txt";
File::makeHtml($filename, $html_data);

// 页码列表
$lastmod = date("Y-m-d\TH:i:s").substr(date("O"), 0, 3).":".substr(date("O"), 3);
$sitemap_pages = array(
	'http://iegole.com',
	'http://www.iegole.com',
);
$__pagesize = 32;
$list = $oGoods->get_list('*', array(), array(), 1, 1);
if(!empty($list)){
	$sitemap_pages[] = array(
		'loc'			=> htmlspecialchars(WEB_URL."goods/index.html"),
		'lastmod'		=> $lastmod,
		'changefreq' 	=> 'daily',
		'priority' 		=> 1,
	);
	$pages = ceil($list[ 'total' ] / $__pagesize);
	for ($i = 1; $i <= $pages; $i ++){
		$sitemap_pages[] = array(
			'loc'			=> htmlspecialchars(WEB_URL."goods/{$i}.html"),
			'lastmod'		=> $lastmod,
			'changefreq' 	=> 'daily',
			'priority' 		=> 1,
		);
	}
}

foreach($categorys['category_list'] as $category_id => $category_info){
	if(empty($category_info['show']))continue;
	$sitemap_pages[] = array(
		'loc'			=> htmlspecialchars(WEB_URL."goods/".$category_info['mark']."/index.html"),
		'lastmod'		=> $lastmod,
		'changefreq' 	=> 'daily',
		'priority' 		=> 1,
	);
	
	$select = array();
	$childs = $oGoodscat->get_category_list($category_info['id']);
	if(empty($childs)){
		$select['catid'] = $category_info['id'];
	}else{
		$select['childs'] = implode(',', array_keys($childs));
	}
	$goods_list = $oGoods->get_list('`id`', $select, array(), 1, 1);
	if(!empty($goods_list['list'])){
		// 页码列表
		$pages = ceil($goods_list['total'] / $__pagesize);
		for($i = 1; $i <= $pages; $i ++){
			$sitemap_pages[] = array(
				'loc'			=> htmlspecialchars(WEB_URL."goods/".$category_info['mark']."/".$i.".html"),
				'lastmod'		=> $lastmod,
				'changefreq' 	=> 'daily',
				'priority' 		=> 1,
			);
		}
	}
}

// 格式化，输出 SiteMap
$sitemap_pages = format_maps($sitemap_pages, false);
$vars['sitemaps'] = $sitemap_pages;
echo $html_data = $T->fetch('goods/goods.sitemapGoogleGoods.xml', null, null, false);
$filename = WEB_PTH."sitemap_google_pages.xml";
File::makeHtml($filename, $html_data);
exit;
/**
 * 格式化
 *
 * @param array $sitemap_pages
 */
function format_maps($sitemap_pages = array(), $index = false){
	$map = array();
	foreach ($sitemap_pages as $url){
		if(is_array($url)){
			$map[] = $url;
			continue;
		}
		$row = array();
		$row['loc']			= htmlspecialchars($url);
		$row['lastmod']		= date("Y-m-d\TH:i:s").substr(date("O"), 0, 3).":".substr(date("O"), 3);
		if(!$index){	// 非索引文件输出
			$row['changefreq'] 	= 'daily';
			$row['priority'] 	= 0.5;
		}
		$map[] = $row;
	}
	return $map;
}
?>